﻿#include "FPCacheDBLoader.h"
#include <QSqlQuery>
#include <QDebug>
#include <QDir>
#include <QStandardPaths>
#include "ZKFingerReader/FingerprintCacheDB.h"

namespace ZKFP
{

FPCacheDBLoader::FPCacheDBLoader(QObject *parent) : QObject(parent)
{
    m_database =  QSqlDatabase::addDatabase("QSQLITE");
    QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
    QDir dir;
    dir.mkdir(path);
    path += "/fingerprintDB.db";
    qDebug() << path;
    m_database.setDatabaseName(path);
    m_database.open();
    QSqlQuery query(m_database);
    query.exec("CREATE TABLE IF NOT EXISTS [fingerprint]("
               "user_id INTEGER NOT NULL UNIQUE,"
               "name TEXT,"
               "password TEXT,"
               "fingerprint BLOB);");

}

QMap<int, QString> FPCacheDBLoader::userNames()
{
    QSqlQuery query(m_database);
    query.setForwardOnly(true);
    query.prepare("SELECT user_id, name FROM fingerprint");
    query.exec();
    QMap<int, QString> names;
    while( query.next( ) )
    {
        int id = query.value(0).toInt();
        QString name = query.value(1).toString();

        names[id] = name;
    }
    return names;
}

QList<int> FPCacheDBLoader::userIDs()
{
    QSqlQuery query(m_database);
    query.setForwardOnly(true);
    query.prepare("SELECT user_id FROM fingerprint");
    query.exec();
    QList<int> IDs;
    while( query.next( ) )
    {
        int id = query.value(0).toInt();
        IDs << id;
    }
    return IDs;
}

bool FPCacheDBLoader::addUser(int userID, QString name, QString password, QByteArray fpTemplate)
{
    QSqlQuery query(m_database);
    query.prepare("INSERT into fingerprint(user_id, name, password, fingerprint) VALUES(:user_id, :name,:password, :fingerprint) ;");
    query.bindValue(":user_id", userID);
    query.bindValue(":name", name);
    query.bindValue(":password", password);
    query.bindValue(":fingerprint", fpTemplate);
    return query.exec();
}

bool FPCacheDBLoader::userInfo(int userID, QString &name, QString &password, QByteArray &fpTemplate)
{
    QSqlQuery query(m_database);
    query.setForwardOnly(true);
    query.prepare("SELECT * FROM fingerprint WHERE user_id = :id");
    query.bindValue(":id", userID);
    query.exec();
    if( query.next( ) )
    {
        name = query.value(1).toString();
        password = query.value(2).toString();
        fpTemplate = query.value(3).toByteArray();
        return true;
    }
    return false;
}

bool FPCacheDBLoader::deleteUser(int userID)
{
    QSqlQuery query(m_database);
    query.prepare("DELETE FROM fingerprint WHERE user_id = :id");
    query.bindValue(":id", userID);
    return query.exec();
}

QString FPCacheDBLoader::userName(int userID)
{
    QSqlQuery query(m_database);
    query.setForwardOnly(true);
    query.prepare("SELECT name FROM fingerprint WHERE user_id = :id");
    query.bindValue(":id", userID);
    query.exec();
    if( query.next( ) )
    {
        QString name = query.value(0).toString();
        return name;
    }
    return QString();
}

void FPCacheDBLoader::initFPCacheDB(FingerprintCacheDB *fpDB)
{
    fpDB->clearAll();
    QSqlQuery query(m_database);
    query.setForwardOnly(true);
    query.exec("SELECT user_id, fingerprint FROM fingerprint");
    while( query.next( ) )
    {
        int fid = query.value(0).toInt();
        QByteArray fpTemplate = query.value(1).toByteArray();
        fpDB->addItem(fid, fpTemplate);
        qDebug() << "fid = " << fid;
    }
}

bool FPCacheDBLoader::loadFromFile(QString fileName, ZKFP::FingerprintCacheDB *fpDB)
{
    QSqlDatabase database = QSqlDatabase::addDatabase("QSQLITE");
    database.setDatabaseName(fileName);

    if(database.open())
    {
        qDebug() << "database open succ";
        fpDB->clearAll();
        QSqlQuery query(database);
        query.setForwardOnly(true);
        query.exec("SELECT user_id, fingerprint FROM fingerprint");
        while( query.next( ) )
        {
            int fid = query.value(0).toInt();
            QByteArray fpTemplate = query.value(1).toByteArray();
            fpDB->addItem(fid, fpTemplate);
        }
        return true;
    }
    return false;
}


}
